package com.doule.farmerassistant;

import android.content.Context;

import org.apache.http.Header;
import org.apache.http.HttpResponse;

/**
 * ToolHelper class.
 * 
 * @author riguang
 * 
 */
public final class ToolHelper {
    /**
     * Constructor.
     */
    private ToolHelper() {
        
    }
    
    /**
     * error code.
     */
    public static final int ERR_SUCCEED = 0;
    /**
     * error code.
     */
    public static final int ERR_DATABASE = 1;
    /**
     * error code.
     */
    public static final int ERR_TOKEN_EXPIRED = 2;
    /**
     * error code.
     */
    public static final int ERR_ACCOUNT_NOT_EXIST = 3;
    /**
     * error code.
     */
    public static final int ERR_PASSWORD_NOT_MATCH = 4;
    /**
     * error code.
     */
    public static final int ERR_ACCOUNT_NEED = 5;
    /**
     * error code.
     */
    public static final int ERR_PASSWORD_NEED = 6;
    /**
     * error code.
     */
    public static final int ERR_ENCODE = 7;
    /**
     * error code.
     */
    public static final int ERR_DECODE = 8;
    
    /**
     * cancel error.
     */
    public static final int ERR_TASK_NOT_CANCEL_ABLE = 32;
    /**
     * task has start.
     */
    public static final int ERR_TASK_ALREADY_START = 33;
    /**
     * not running.
     */
    public static final int ERR_TASK_NOT_RUNNING = 34;
    /**
     * not belong to account.
     */
    public static final int ERR_TASK_NOT_BELONG2_ACCOUNT = 35;
    /**
     * task not exist.
     */
    public static final int ERR_TASK_NOT_EXIST = 36;
    /**
     * controller not valid.
     */
    public static final int ERR_TASK_CONTROLLER_NOT_VALID = 37;
    /**
     * controller is busy.
     */
    public static final int ERR_TASK_CONTROLLER_BUSY = 38;
    /**
     * task send command 2 controller failed.
     */
    public static final int ERR_TASK_SEND2_CONTROLLER = 39;
    /**
     * task timeout.
     */
    public static final int ERR_TASK_TIMEOUT = 40;

    /**
     * Get error string.
     * 
     * @param context
     *            Context
     * @param err
     *            Error code string.
     * @return Error String.
     */
    public static String getErrString(final Context context, final int err) {
        String errStr = "";
        switch(err) {
        case ERR_SUCCEED:
            errStr = context.getString(R.string.ERR_SUCCEED);
            break;
        case ERR_DATABASE:
            errStr = context.getString(R.string.ERR_DATABASE);
            break;
        case ERR_TOKEN_EXPIRED:
            errStr = context.getString(R.string.ERR_TOKEN_EXPIRED);
            break;
        case ERR_ACCOUNT_NOT_EXIST:
            errStr = context.getString(R.string.ERR_ACCOUNT_NOT_EXIST);
            break;
        case ERR_PASSWORD_NOT_MATCH:
            errStr = context.getString(R.string.ERR_PASSWORD_NOT_MATCH);
            break;
        case ERR_ACCOUNT_NEED:
            errStr = context.getString(R.string.ERR_ACCOUNT_NEED);
            break;
        case ERR_PASSWORD_NEED:
            errStr = context.getString(R.string.ERR_PASSWORD_NEED);
            break;
        case ERR_ENCODE:
            errStr = context.getString(R.string.ERR_ENCODE);
            break;
        case ERR_DECODE:
            errStr = context.getString(R.string.ERR_DECODE);
            break;
        case ERR_TASK_NOT_CANCEL_ABLE:
            errStr = context.getString(R.string.ERR_TASK_NOT_CANCEL_ABLE);
            break;
        case ERR_TASK_ALREADY_START:
            errStr = context.getString(R.string.ERR_TASK_ALREADY_START);
            break;
        case ERR_TASK_NOT_RUNNING:
            errStr = context.getString(R.string.ERR_TASK_NOT_RUNNING);
            break;
        case ERR_TASK_NOT_BELONG2_ACCOUNT:
            errStr = context.getString(R.string.ERR_TASK_NOT_BELONG2_ACCOUNT);
            break;
        case ERR_TASK_NOT_EXIST:
            errStr = context.getString(R.string.ERR_TASK_NOT_EXIST);
            break;
        case ERR_TASK_CONTROLLER_NOT_VALID:
            errStr = context.getString(R.string.ERR_TASK_CONTROLLER_NOT_VALID);
            break;
        case ERR_TASK_CONTROLLER_BUSY:
            errStr = context.getString(R.string.ERR_TASK_CONTROLLER_BUSY);
            break;
        case ERR_TASK_SEND2_CONTROLLER:
            errStr = context.getString(R.string.ERR_TASK_SEND2_CONTROLLER);
            break;
        case ERR_TASK_TIMEOUT:
            errStr = context.getString(R.string.ERR_TASK_TIMEOUT);
            break;
        default:
            errStr = context.getString(R.string.ERR_UNKNOWN);
            break;
        }
        return errStr;
    }
    
    /**
     * Http error.
     * @author Happy
     *
     */
    public static class HttpError {
        /**
         * construct.
         * @param code code
         * @param msg message
         */
        public HttpError(int code, String msg) {
            mCode = code;
            mMsg = msg;
        }
        
        /**
         * code.
         */
        private final int mCode;
        /**
         * message.
         */
        private final String mMsg;
        /**
         * get code.
         * @return code
         */
        public int getCode() {
            return mCode;
        }
        /**
         * get message.
         * @return message
         */
        public String getMsg() {
            return mMsg;
        }
    }
    
    /**
     * parse error information.
     * @param c context
     * @param resp HTTP response
     * @return HttpError object
     */
    public static HttpError errorParse(Context c, HttpResponse resp) {
        Header header = resp.getFirstHeader("error");
        int code = 0;
        if (header != null) {
            code = Integer.parseInt(header.getValue());
        }
        
        if (code == 0) {
            return null;
        }
        
        String msg = null;
        header = resp.getFirstHeader("errmsg");
        if (header != null) {
            msg = header.getValue();
        }
        
        if (msg == null) {
            msg = getErrString(c, code);
        }
        
        return new HttpError(code, msg);
    }
}
